<template>
  <div>
    <el-dialog :title="info.isAdd?'菜单添加':'菜单编辑'" :visible.sync="info.isshow" @closed="close">
      <el-form :model="form" >
        {{ form }}
        <el-form-item label="菜单名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" label-width="120px" >
          <el-select v-model="form.pid" @change="changePid">
            <el-option value disabled>请选择</el-option>
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" label-width="120px">
          <template>
            <el-radio v-model="form.type" :label="1" disabled>目录</el-radio>
            <el-radio v-model="form.type" :label="2" disabled>菜单</el-radio>
          </template>
        </el-form-item>
        <el-form-item
          label="菜单图标"
          label-width="120px"
          v-if="form.type == 1">
          <el-select v-model="form.icon">
            <el-option value disabled>请选择</el-option>
            <!-- 遍历图标 -->
            <el-option :value="item" v-for="item in icons" :key="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单地址" label-width="120px" v-else>
          <el-select v-model="form.url">
            <el-option value disabled>请选择</el-option>
            <el-option
              v-for="item in IndexRouters"
              :key="item.path"
              :label="item.meta.title"
              :value="'/' + item.path"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="add" v-if="info.isAdd">添 加</el-button>
        <el-button type="primary" v-else @click="xuigai">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { IndexRouters } from "../../../router";
import { reqmenuadd, reqmenuedit, reqmenuinfo } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info", "list"],
  data() {
    return {
      form: {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      //图标集合
      icons: [
        "el-icon-s-tools",
        "el-icon-user",
        "el-icon-s-fold",
        "el-icon-message-solid",
      ],
      //路由集合
      IndexRouters,
    };
  },
  methods: {
    quxiao() {
      this.info.isshow = false;
    },
    qingkong() {
      this.form = {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    //添加
    add() {
      reqmenuadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.qingkong();
          this.info.isshow = false;
          this.$emit("init");
        }
      });
    },
    // 修改pid
    changePid() {
      this.form.type = this.form.pid == 0 ? 1 : 2;
    },
    //获取一条数据
    getOne(id) {
      reqmenuinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.id = id;
        }
      });
    },
    //修改
    xuigai() {
      reqmenuedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.qingkong();
          this.info.isshow = false;
          this.$emit("init");
        }
      });
    },
    //弹框关闭
    close() {
      !this.info.isAdd && this.qingkong();
    },
  },
};
</script>

<style>
</style>